package cn.ztuo.bitrade.dao;

import cn.ztuo.bitrade.dao.base.BaseDao;
import cn.ztuo.bitrade.entity.Award;
import cn.ztuo.bitrade.entity.CesspitAward;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


public interface CesspitAwardDao extends BaseDao<CesspitAward> {

    @Query(value = "SELECT ca.id,ca.mobile,ca.create_time,ca.address,ca.type,ca.status,SUM(ca.number) number FROM cesspit_award ca WHERE ca.status =1 and ca.type <=:type GROUP BY ca.mobile ",nativeQuery = true)
    List<CesspitAward> findAllGroupBy(@Param("type") Long type);

    @Query(value = "SELECT ca.id,ca.mobile,ca.create_time,ca.address,ca.type,ca.status,SUM(ca.number) number FROM cesspit_award ca WHERE ca.status =1 and ca.type <=:type and ca.mobile =:mobile and to_days(ca.create_time) = to_days(now()) GROUP BY ca.mobile ",nativeQuery = true)
    CesspitAward findAllGroupByTwo(@Param("type") Long type,@Param("mobile") String mobile);

    @Query(value = "select sum(ca.number) from CesspitAward ca where TO_DAYS(ca.createTime) = TO_DAYS(NOW())")
    BigDecimal findAllSum();

    @Transactional
    @Modifying
    @Query(value = "update CesspitAward set status = 2 where mobile =:mobile and status = 1")
    int updateStatus(@Param("mobile") String mobile);
}
